package cn.iocoder.an.module.mold.controller.admin.recheckcat2item;

import cn.iocoder.an.module.mold.controller.admin.recheckcat2item.vo.RecheckCat2itemSaveReqVO;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.recheckcat2item.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.recheckcat2item.RecheckCat2itemDO;
import cn.iocoder.an.module.mold.service.recheckcat2item.RecheckCat2itemService;

@Tag(name = "管理后台 - 模具复检分类项目对照")
@RestController
@RequestMapping("/mold/recheck-cat2item")
@Validated
public class RecheckCat2itemController {

    @Resource
    private RecheckCat2itemService recheckCat2itemService;

    @PostMapping("/create")
    @Operation(summary = "创建模具复检分类项目对照")
    @PreAuthorize("@ss.hasPermission('mold:recheck-cat2item:create')")
    public CommonResult<Long> createRecheckCat2item(@Valid @RequestBody RecheckCat2itemSaveReqVO createReqVO) {
        return success(recheckCat2itemService.createRecheckCat2item(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具复检分类项目对照")
    @PreAuthorize("@ss.hasPermission('mold:recheck-cat2item:update')")
    public CommonResult<Boolean> updateRecheckCat2item(@Valid @RequestBody RecheckCat2itemSaveReqVO updateReqVO) {
        recheckCat2itemService.updateRecheckCat2item(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具复检分类项目对照")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:recheck-cat2item:delete')")
    public CommonResult<Boolean> deleteRecheckCat2item(@RequestParam("id") Long id) {
        recheckCat2itemService.deleteRecheckCat2item(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具复检分类项目对照")
                @PreAuthorize("@ss.hasPermission('mold:recheck-cat2item:delete')")
    public CommonResult<Boolean> deleteRecheckCat2itemList(@RequestParam("ids") List<Long> ids) {
        recheckCat2itemService.deleteRecheckCat2itemListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具复检分类项目对照")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:recheck-cat2item:query')")
    public CommonResult<RecheckCat2itemRespVO> getRecheckCat2item(@RequestParam("id") Long id) {
        RecheckCat2itemDO recheckCat2item = recheckCat2itemService.getRecheckCat2item(id);
        return success(BeanUtils.toBean(recheckCat2item, RecheckCat2itemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具复检分类项目对照分页")
    @PreAuthorize("@ss.hasPermission('mold:recheck-cat2item:query')")
    public CommonResult<PageResult<RecheckCat2itemRespVO>> getRecheckCat2itemPage(@Valid RecheckCat2itemPageReqVO pageReqVO) {
        PageResult<RecheckCat2itemDO> pageResult = recheckCat2itemService.getRecheckCat2itemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecheckCat2itemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具复检分类项目对照 Excel")
    @PreAuthorize("@ss.hasPermission('mold:recheck-cat2item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecheckCat2itemExcel(@Valid RecheckCat2itemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecheckCat2itemDO> list = recheckCat2itemService.getRecheckCat2itemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具复检分类项目对照.xls", "数据", RecheckCat2itemRespVO.class,
                        BeanUtils.toBean(list, RecheckCat2itemRespVO.class));
    }

    @GetMapping("/list-catid")
    @Operation(summary = "获得模具复检分类项目对照")
    @Parameter(name = "catId", description = "类型编号", required = true, example = "1024")
    @TransMethodResult
    public CommonResult<List<RecheckCat2itemRespVO>> getRecheckCat2itemByCatId(@RequestParam("catId") Long catId) {
        System.out.println("catId:"+catId);
        List<RecheckCat2itemDO> list = recheckCat2itemService.getRecheckCat2itemByCatId(catId);
        return success(BeanUtils.toBean(list, RecheckCat2itemRespVO.class));
    }


    @DeleteMapping("/delete-catid")
    @Operation(summary = "删除模具分类项目对照")
    @Parameter(name = "catId", description = "编号", required = true)
    public CommonResult<Boolean> deleteRecheckCat2itemByCatId(@RequestParam("catId") Long catId) {
        recheckCat2itemService.deleteRecheckCat2itemByCatId(catId);
        return success(true);
    }

    @PostMapping("/update-catid")
    @Operation(summary = "更新模具复检分类项目对照")
    public CommonResult<Boolean> updateRecheckCat2itemByCatId(@Valid @RequestBody List<RecheckCat2itemSaveReqVO> updateReqVO) {
        // 调用服务层方法处理更新逻辑
        recheckCat2itemService.updateRecheckCat2itemByCatId(updateReqVO);
        return success(true);
    }
}